package com.cobalt4nt.photomap;

import java.io.IOException;
import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;

import com.cobalt4nt.photomap.MapActivity.PictureWithLocation;
import com.google.android.gms.maps.model.LatLng;

public class UnGeocoderTask extends AsyncTask<PictureWithLocation, Integer, PictureWithLocation> {
	GeocodingActivity context;

	public UnGeocoderTask(GeocodingActivity context) {
		this.context = context;
	}
	@Override
	protected PictureWithLocation doInBackground(PictureWithLocation... arg0) {
		PictureWithLocation pictureLocation = arg0[0];
		LatLng latLong = pictureLocation.latLong;
		List<Address> addressList;
		try {
			Geocoder geocoder = new Geocoder(context);
			addressList = geocoder.getFromLocation(latLong.latitude, latLong.longitude, 1);
		} catch(IOException e) {
			//TODO: Add error method
			context.error("No Internet Connection");
			return null;
		}
		if(addressList.size() > 0) {
			pictureLocation.address = addressList.get(0);
			pictureLocation.hasAddress = true;
		} else {
			pictureLocation.address = null;
			pictureLocation.hasAddress = false;
		}

		return pictureLocation;
	}

	protected void onPostExecute(PictureWithLocation result) {
		context.alertUnGeocodingComplete(result);
	}
}